package com.wsh.sms.controller;

import com.wsh.sms.service.AccountService;
import com.wsh.sms.utils.AccountAccess;
import com.wsh.sms.utils.AccountExist;
import com.wsh.sms.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wsh
 */
@RestController
@RequestMapping("/account")
@CrossOrigin
public class AccountController {

    private final AccountService accountService;

    @Autowired
    public AccountController(AccountService accountService){
        this.accountService = accountService;
    }

    @RequestMapping(value = "/login-super")
    public Result getAccount(@RequestParam(name = "username") String username,@RequestParam(name = "password") String password){
        String data =  accountService.getAccountSuper(username,password);
        if(data == null){
            return new Result<>(404,"not-found","账号不存在或者密码错误");
        }
        if(AccountAccess.SUPER.getAccess().equals(data)){
            return new Result<>(200,"success",data);
        }
        else {
            return new Result<>(200,"fail",data);
        }

    }

    @RequestMapping(value = "/existAccount")
    public Result existAccount(@RequestParam(name = "username") String username){
        String data = accountService.existAccount(username);
        if (AccountExist.EXIST.getExist().equals(data)){
            return new Result<>(200,"fail","账号存在，请重新输入");
        }
        else {
            return new Result<>(200,"success","账号可以使用");
        }
    }

    @RequestMapping(value = "/add-common-account")
    public Result addCommonAccount(@RequestParam(name = "username") String username,@RequestParam(name = "password") String  password){
        int result = 0;
        try{
            result = accountService.addCommonAccount(username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (result >= 1){
            return new Result<>(200,"success","注册成功");
        }
        else {
            return new Result<>(200,"fail","注册失败");
        }
    }

    @RequestMapping(value = "/get-number")
    public Result getNumber(@RequestParam(name = "username") String username){
        String data = accountService.getNumber(username);
        if (data != null){
            return new Result<>(200,"success",data);
        }
        else {
            return new Result<>(200,"fail","请绑定工号");
        }
    }

    @RequestMapping(value = "/blind-number")
    public Result getNumber(@RequestParam(name = "number") String number,@RequestParam(name = "username") String username){
        int result = 0;
        try{
            result = accountService.blindNumber(number,username);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (result >= 1){
            return new Result<>(200,"success","绑定成功");
        }
        else {
            return new Result<>(200,"fail","绑定失败");
        }
    }

}
